package com.speedata.testsimple;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.speedata.uhftest.R;
import com.speedata.utils.ToastUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author xuyan  李晓华  imbox buzzer
 */
public class ImboxBuzzerActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;

    private Button bt3;
    private Button bt4;

    private Button bt5;
    private Button bt6;

    private Button bt7;
    private Button bt8;

    private Button bt9;
    private Button bt10;

    //gpio2_a0 gpio2_a1 gpio2_a2 gpio2_a5
    private String path1 = "sys/class/pwm/pwmchip0/export";
    private String path2 = "sys/class/pwm/pwmchip0/pwm0/period";
    private String path3 = "sys/class/pwm/pwmchip0/pwm0/duty_cycle";
    private String path4 = "sys/class/pwm/pwmchip0/pwm0/polarity";
    private String path5 = "sys/class/pwm/pwmchip0/pwm0/enable";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imbox_buzzer);
        initView();
    }

    @Override
    protected void onPause() {
        //写0
        open(path5, "0");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        //ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/pwm0/enable 写0");
        super.onDestroy();
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);
        bt9 = findViewById(R.id.bt9);
        bt10 = findViewById(R.id.bt10);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        bt9.setOnClickListener(this);
        bt10.setOnClickListener(this);

    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                open(path1, "0");
                ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/export 写0");
                break;
            case R.id.bt2:
                open(path1, "0");
                ToastUtils.showLongToastSafe("gpio2_a0 OFF");
                break;
            case R.id.bt3:
                open(path2, "366000");
                ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/pwm0/period 写366000");
                break;
            case R.id.bt4:
                open(path2, "0");
                ToastUtils.showLongToastSafe("gpio2_a0 OFF");
                break;
            case R.id.bt5:
                open(path3, "183000");
                ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/pwm0/duty_cycle 写183000");
                break;
            case R.id.bt6:
                open(path3, "0");
                ToastUtils.showLongToastSafe("gpio2_a0 OFF");
                break;
            case R.id.bt7:
                open(path4, "normal");
                ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/pwm0/polarity 写normal");
                break;
            case R.id.bt8:
                open(path4, "0");
                ToastUtils.showLongToastSafe("gpio2_a0 OFF");
                break;
            case R.id.bt9:
                open(path5, "1");
                ToastUtils.showLongToastSafe("/sys/class/pwm/pwmchip0/pwm0/enable 写1");
                break;
            case R.id.bt10:
                open(path5, "1");
                ToastUtils.showLongToastSafe("gpio2_a0 OFF");
                break;
            default:
                break;
        }
    }


    private static BufferedWriter TorchFileWrite;

    /**
     * imbox,buzzer
     *
     * @param str 各种入参
     */
    public static void open(String path, String str) {
        File TorchFileName = new File(path);
        try {
            TorchFileWrite = new BufferedWriter(new FileWriter(TorchFileName, false));
            TorchFileWrite.write(str);
            TorchFileWrite.flush();
            TorchFileWrite.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
